﻿using System;
using System.Reflection;
using System.ComponentModel;
using System.Text.RegularExpressions;

namespace Optum.SN.Infra.Core.Cryptography
{
    /// <summary>
    /// RegEx patterns.
    /// </summary>
    public enum RexExPattern
    {
        /// <summary>
        /// Numeric pattern.
        /// </summary>
        [Description(@"^\d+$")] Numeric
    }
    /// <summary>
    /// General purpose utility.
    /// </summary>
    public static class Utility
    {
        /// <summary>
        /// Determines if a data element is encrypted.
        /// </summary>
        /// <param name="data">The data element to check for encryption.</param>
        /// <param name="plainTextPattern">The pattern the plain text data is expected to conform to.</param>
        /// <returns>True when data content is a mismatch, otherwise false.</returns>
        public static bool IsDataEncrypted(string data, RexExPattern plainTextPattern)
        {
            return !string.IsNullOrEmpty(data) && data.Trim().Length > 0 &&
                   !Regex.IsMatch(data, GetEnumDescription(plainTextPattern));
        }
        /// <summary>
        /// Gets attribute description of specified Enum item.
        /// </summary>
        /// <param name="value">The Enum item to get description of.</param>
        /// <returns>Description attribute value when available, otherwise the string representation of the Enum.</returns>
        private static string GetEnumDescription(Enum value)
        {
            FieldInfo fi = value.GetType().GetField(value.ToString());
            DescriptionAttribute[] attributes =
                (DescriptionAttribute[]) fi.GetCustomAttributes(typeof (DescriptionAttribute), false);
            return attributes.Length > 0 ? attributes[0].Description : value.ToString();
        }
    }
}